< previous page page_631 next page >

Page 631
If the light ray is leaving the object, the geometry of the situation is slightly different from that shown in the previous figures. In that case, all you need to do to make the equations work out is to switch the sign of Cos(q). Because you compute this value using a dot product, Cos(q) becomes (N V).
This method works quite well under normal circumstances in which most objects are surrounded by "air." It does not handle cases in which one transparent object partially overlaps another, however. When a light ray enters one of the objects, the use of N1 and N2 is correct. If the ray then passes from the first object into the second, the values of N1 and N2 for the second object are probably wrong.
For example, suppose your scene includes a glass cylinder overlapping a glass sphere. You should set N1 = 1.0 (air) and N2 = 1.4 (glass) for both objects. If a light ray crosses through the cylinder directly into the sphere, there should be no refraction. Because both objects are made of glass, the ray should pass through without changing direction. Because N1 = 1.0 and N2 = 1.4 for the sphere, however, the ray will be incorrectly refracted.
For many scenes, this restriction is not too great a burden. Even if you do create overlapped objects like this, the resulting reflections and refractions are usually complicated enough that it is difficult to notice that the image is not exactly as it should be. If you really want the calculations to be correct, modify this simple method for computing refraction so that the ray tracing routine always knows what kind of substance it is passing through and what kind of substance it is entering.
The following code shows the parts of the CalculateHitColor subroutine that deal with transmitted light.
e10a6a0a59f035e870d90cca05b85fce.gif
' Return the red, green, and blue components of
' an object at a hit position (px, py, pz) with
' normal vector (nx, ny, nz).
Public Sub CalculateHitColor(ByVal depth As Integer, _
    Objects As Collection, . . .)
        :
    ' Consider each light source.
    For Each light_source In LightSources
            :
        ' **********************
        ' * Direct Transmitted *
        ' **********************
        ' See if the light and viewpoint are on
        ' opposite sides of the surface and if we
        ' are not in shadow.
        If is_transparent Then
            ' Find LT, the light transmission vector.
            cos1 = Abs(LdotN)
            n1_over_n2 = n1 / n2
            cos2 = Sqr(1 - (1 - cos1 * cos1) * n1_over_n2 * _
                n1_over_n2)
            ' Note that the incident vector I = -L.
            normal_factor = cos2 - n1_over_n2 * cos1
            ltx = -n1_over_n2 * nlx - normal_factor * Nx

 
< previous page page_631 next page >